/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package SourceCode;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

/**
 *
 * @author ssaad
 */
public class Member implements Serializable
{
    private int MemberID;
    private String NAme;
    private String Address;
    private String phone;
    public  ArrayList<Reservation> reservations = new ArrayList<Reservation>();
    public  ArrayList<Loan> loans = new ArrayList<Loan>();

    public Member(int MemberID, String NAme, String Address, String phone) {
        this.MemberID = MemberID;
        this.NAme = NAme;
        this.Address = Address;
        this.phone = phone;
    }

    public String getAddress() {
        return Address;
    }

    public int getMemberID() {
        return MemberID;
    }

    public String getNAme() {
        return NAme;
    }

    public String getPhone() {
        return phone;
    }

    public void setAddress(String Address) {
        this.Address = Address;
    }

    public void setMemberID(int MemberID) {
        this.MemberID = MemberID;
    }

    public void setNAme(String NAme) {
        this.NAme = NAme;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public Reservation reserveItem(Item item)
    {
        //check that there is no available copies of item
        for(Copy c : item.Copies)
        {
            if(c.IsAvailable())
                return null;
        }
        //reserve item now
        Calendar c = Calendar.getInstance();
        Reservation reservation ;
        reservation = new Reservation(this, item, new Date());
        reservations.add(reservation);
        return reservation;
    }
   public Loan borrow(Copy copy)
   {
       Loan loan ;
       Calendar c = Calendar.getInstance();
        if(copy.IsAvailable() && copy.IsReserved() == false  )
        {
            loan = new Loan(copy, this, c.getTime());
            loans.add(loan);
            copy.borrow(loan);

            //borrow success for this item copy
            //remove member reservation of that item
            Reservation reservation = new Reservation(this, copy.getItem(), new Date());
            reservation.RemoveReservation();
            return loan;
        }
        else if(copy.IsAvailable() && copy.IsReserved() == true  )
        {
            if(copy.getItem().IsReservedBy(this))
            {
                loan = new Loan(copy, this, c.getTime());
                loans.add(loan);
                copy.borrow(loan);

                //borrow success for this item copy
                //remove member reservation of that item
                Reservation reservation = new Reservation(this, copy.getItem(), new Date());
                reservation.RemoveReservation();
                return loan;
            }
            return null;
        }
       else
       {
           System.err.println("the copy is no available for borrowing");
           return null;
       }
    }

   public  boolean returncopy(Copy copy)
   {
       boolean isReturned= false;
       for(Loan loan : loans)
       {
           if(loan.getCopy() == copy)
           {
               isReturned = true;
               copy.returncopy();
               loans.remove(loan);
               break;
           }
       }
      return isReturned;



   }

   public String tostring()
   {
       return "[ Name:" +this.NAme + " ID:" + this.MemberID + " Address:" + this.Address + " Phone:" + this.phone + "]";
   }

   /*
     * return Item list of  reservation list of one Member
     */
    public ArrayList<Item> ItemReservationList()
    {
        ArrayList<Item> items = new ArrayList<Item>();

        for(Reservation  reserveI : reservations)
        {
            items.add(reserveI.getItem());
        }
        return items;
    }

}
